﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CetWin.Phone.Entity;
using System.Linq;
using System.Collections.Generic;
namespace CetWin.Phone.Services
{
    public class CategoryService:BaseDataService
    {
        public void AddCategory(Category entity)
        {
            entity.Type = Category.NormalType;
            db.Categories.InsertOnSubmit(entity);
            Update();
        }

        public void Delete(Category entity)
        {
            db.Categories.DeleteOnSubmit(entity);
            Update();
        }

        public IEnumerable<Category> GetNormalCategory()
        {
            return db.Categories.Where(e => e.Type == Category.NormalType);
        }

        public IEnumerable<Category> GetDateCategory()
        {
            var list = db.Categories.Where(e => e.Type == Category.DateType);
            //foreach (var item in list)
            //{
            //    var list2 = new List<CategoryWord>();
            //    for (int i = 0; i < 2; i++)
            //    {
            //        list2.Add(new CategoryWord() { WordName = "xxxxxxxxxxxxxxxxxxx" });
            //    }
            //    item.Words = list2;
            //}
            return list;
        }

        public IEnumerable<CategoryWord> GetCategoryWord(Category cat)
        {
            return db.CategoryWords.Where(e => e.CatId == cat.Id);
        }

        public void AddTodayWord(Favorite favorite)
        {
            var now=DateTime.Now.ToString("yyyy-MM-dd");
            Category cat = null;
            if (db.Categories.Any(e => e.Name == now))
            {
                cat = db.Categories.FirstOrDefault(e => e.Name == now);
            }
            else
            {
                cat = new Category() { Name = now,Type=Category.DateType };
                db.Categories.InsertOnSubmit(cat);
                db.SubmitChanges();
            }
            if (!db.CategoryWords.Any(e => e.WordName == favorite.Name))
            {
                var catword = new CategoryWord() { CatId = cat.Id, WordName = favorite.Name };
                db.CategoryWords.InsertOnSubmit(catword);
                db.Favorites.DeleteOnSubmit(favorite);
                db.SubmitChanges();
            }
        }
    }
}